<?php 
/**
 *
 *
 */
include_once('User.php');
include_once('db_connect.php');

class Status {

	private $statusID;
	private $userID;
	private $content;
	private $destroy;

	function Status() {
		$this->userID = 0;
		$this->content = "";
		$this->destroy = 0;
	}

	function connect() {
		$this->connect = new Connect();
		$this->mysqli = $this->connect->getMysqli();
	}

	function getsStatusID() {
		return $this->statusID;
	}

	function setStatusID($value) {
		$this->statusID = $value;
	}

	function getUserID() {
		return $this->userID;
	}

	function setUserID($value) {
		$this->userID = $value;
	}

	function getContent() {
		return $this->content;
	}

	function setContent($value) {
		$this->content = $value;
	}

	function getDestroy() {
		return $this->destroy;
	}

	function setDestroy($value) {
		$this->destroy = $value;
	}

	/**
	 *	Function get Statuss of user
	 *	- input: $userstatusID
	 *	- output: Statuss list
	 *
	 */
	function getStatussList($userID) {

		$this->connect();

		$sql = "SELECT Content FROM STATUS WHERE (UserID = ?) AND (Destroy = 0)";
		// Prepare statement
		$prSelect = $this->mysqli->prepare($sql);

		if($prSelect) {
			$prSelect->bind_param('i', $userID);
		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. ") " .$this->mysqli->error;
		}

		//Execute
		if (!$prSelect->execute()) {
			echo "Execute Failed: (" .$prSelect->errno. ") " .$prSelect->error;
		}

		$statuss = array();
		$i = 0;

		$result = $prSelect->get_result();

		while($row = $result->fetch_assoc()) {
			$status = new Status();

			$status->setUserID($userID);
			$status->setContent($row['Content']);

			$statuss[$i] = $status;
			$i++;
		}
		
		return $statuss;
	}

	function Insert($status) {

		$this->connect();

		$userID = $status->getUserID();
		$content = $status->getContent();
 		$destroy = $status->getDestroy();

		$sql = "INSERT INTO Status 
				(UserID, Content, Destroy) 
				VALUES 
				(?, ?, ?)";

		$prInsert = $this->mysqli->prepare($sql);

		if($prInsert) {
			//Bind
			$prInsert->bind_param('isi', 
									$userID,
									$content,
									$destroy
								);
		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. ") " .$this->mysqli->error;
		}

		//Execute
		if ($prInsert->execute()) {
			//Execute successfull
		} else {
			echo "Execute Failed: (" .$prSelect->errno. ") " .$prSelect->error;
		}
	}

	function Update($status) {

		$this->connect();

		$sql = "UPDATE Status SET
				Content = ?,
				Destroy = ?
				WHERE
				UserID = ?";

		$prInsert = $this->mysqli->prepare($sql);

		if($prInsert) {
			//Bind
			$prInsert->bind_param('sii', 
									$Status->getContent(),
									$Status->getDestroy(),
									$Status->getUserID()
								);
		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. ") " .$this->mysqli->error;
		}

		//Execute
		if ($prSelect->execute()) {
			//Execute successfull
		} else {
			echo "Execute Failed: (" .$prSelect->errno. ") " .$prSelect->error;
		}
	}
}
?>